
const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  },
  visitor: {
    token: 'visitor-token'
  }
}
// 路由
const roles = require('./role/routes')
// 用户列表
const userlist = require('./userlist')

module.exports = [
  // user login
  {
    url: '/user/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          _RejCode: '60204',
          _RejMsg: '用户名或密码错误'
        }
      }

      return {
        _RejCode: '00000000',
        _RejMsg: '请求成功',
        data: token
      }
    }
  },

  // user logout
  {
    url: '/user/logout',
    type: 'post',
    response: _ => {
      return {
        _RejCode: '00000000',
        _RejMsg: 'success'
      }
    }
  },

  // 获取用户列表
  {
    url: '/user/list',
    type: 'get',
    response: config => {
      const { query, pagenum = 1, pagesize } = config.query
      console.log(query, pagenum, pagesize)
      let users = userlist.slice(
        (pagenum - 1) * pagesize,
        pagenum * pagesize
      )
      let len = userlist.length
      if (query) {
        users = users.filter(item => item.roleName.includes(query) || item.username.includes(query))
        len = users.length
      }
      return {
        _RejCode: '00000000',
        _RejMsg: '获取用户列表成功',
        data: {
          total: len,
          pagenum: pagenum,
          users: users
        }
      }
    }
  },

  // 改变用户状态， false为不可登录
  {
    url: '/user/\.*/state/\.*',
    type: 'put',
    response: config => {
      const { id, state } = config.body
      let user = {}
      userlist.forEach(item => {
        if (item.id * 1 === id * 1) {
          item.state = state
          user = item
        }
      })
      return {
        _RejCode: '00000000',
        _RejMsg: '设置状态成功',
        data: user
      }
    }
  },

  // 添加用户
  {
    url: '/user/add',
    type: 'post',
    response: config => {
      console.log(config)
      const {
        email,
        mobile,
        password,
        username
      } = config.body
      const user = {
        id: userlist.length + 1,
        email,
        mobile,
        password,
        username,
        state: true,
        roleName: '用户',
        createTime: new Date().getTime()
      }
      userlist.push(user)
      return {
        _RejCode: '00000000',
        _RejMsg: '设置状态成功',
        data: user
      }
    }
  },

  // 根据id获取用户信息
  {
    url: '/user/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      const user = userlist.find(item => item.id * 1 === id * 1)
      return {
        _RejCode: '00000000',
        _RejMsg: '获取成功',
        data: user
      }
    }
  },

  // 修改用户信息
  {
    url: '/user/edit',
    type: 'put',
    response: config => {
      const { id, email, mobile } = config.body
      let user = {}
      userlist.forEach(item => {
        if (item.id * 1 === id * 1) {
          item.email = email
          item.mobile = mobile
          user = item
        }
      })
      return {
        _RejCode: '00000000',
        _RejMsg: '更新成功',
        data: user
      }
    }
  },

  // 删除用户信息
  {
    url: '/user/delete',
    type: 'delete',
    response: config => {
      const { id } = config.body
      const index = userlist.findIndex(item => item.id * 1 === id * 1)
      userlist.splice(index, 1)
      return {
        _RejCode: '00000000',
        _RejMsg: '删除成功',
        data: null
      }
    }
  },
  // update role更新用户对应角色
  {
    url: '/user/roles/update',
    type: 'put',
    response: config => {
      const { userId, roleId } = config.body
      let user = {}
      const roleName = roles.find(item => item.roleId * 1 === roleId * 1).roleName
      userlist.forEach(element => {
        if (element.id * 1 === userId * 1) {
          element.roleName = roleName
          user = element
        }
      })
      return {
        _RejCode: '00000000',
        _RejMsg: '更新用户角色成功',
        data: user
      }
    }
  }
]

